package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-27
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划m
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        //查询护理计划基础信息
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        //查询关联表
        List<NursingProjectPlanVo> projectPlans =nursingProjectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(projectPlans);
        return nursingPlanVo;

    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto dto) {
        //新增护理计划基础 信息
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        boolean result = save(nursingPlan);
        //批量新增护理计划项目信息
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        if (projectPlans != null && !projectPlans.isEmpty()) {
            for (NursingProjectPlan projectPlan : projectPlans) {
                projectPlan.setPlanId(nursingPlan.getId());
            }
            nursingProjectPlanMapper.insertBetch(projectPlans);
        }
        return result?1:0;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto dto) {
//修改基础信息
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        boolean result = updateById(nursingPlan);
        //删除要修改的项目信息
        if(dto.getProjectPlans() != null && !dto.getProjectPlans().isEmpty()){
            nursingProjectPlanMapper.deleteByPlanId(dto.getId());
            //批量新增护理计划项目信息
            List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
            if (projectPlans != null && !projectPlans.isEmpty()) {
                for (NursingProjectPlan projectPlan : projectPlans) {
                    projectPlan.setPlanId(nursingPlan.getId());
                }
                nursingProjectPlanMapper.insertBetch(projectPlans);
            }
        }
        return result?1:0;
    }


    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 获取所有护理计划
     * @return
     */
    @Override
    public List<NursingPlan> getAll() {
        return nursingPlanMapper.getAll();
    }
}
